// pages/Manufactor/Manufactor.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cont:"",
    userList: [],
    searchuserList:[],
    userNamesearch:"",
    isshow:true,
    isshowTow:false
  },
  goAddUser() {
    wx.navigateTo({
      url: "/pages/AddUser/AddUser",
    })
  },
  onSearch(event){
    this.setData({
      userNamesearch: event.detail // 获取输入框内容
    });
    const searchResult = this.data.userList.filter(item =>
      item.username.includes(this.data.userNamesearch)
    );
    if (searchResult.length == 0) {
      this.setData({
        searchuserList: []
      })
    } else {
      this.setData({
        searchuserList: searchResult, // 更新搜索结果
        isshow: false,
        isshowTow: true
      });
    }
  },
  onCancel(){
    this.setData({
      searchuserList: [], // 更新搜索结果
      isshow: true,
      isshowTow: false,
      userNamesearch:''
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log("进入User")

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: "03"
      })
    };
    this.fetchuserList();

  },

  goUserOrderPage(event) {
    // console.log(event.currentTarget.dataset.item)
    const items = event.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/UserOrderPage/UserOrderPage?item=${JSON.stringify(items)}`,
    })
  },

  //gitUserList
  fetchuserList: function () {
    wx.cloud.callFunction({
      name: 'gitUserList',
      success: res => {
        if (res.result.success) {
          // Sort the data based on orderCount in descending order
          const sortedData = res.result.data.sort((a, b) => b.orderCount - a.orderCount);
          
          // Filter the data to count items with orderCount not equal to zero
          const nonZeroOrderCount = sortedData.filter(item => item.orderCount !== 0);
  
          this.setData({
            userList: sortedData,
            cont: nonZeroOrderCount.length
          });
  
          console.info("获取的user信息", this.data.userList);
          console.info("获取的user信息", this.data.cont);
        } else {
          wx.showToast({
            title: 'Failed to fetch data',
            icon: 'none'
          });
        }
      },
      fail: err => {
        console.error('Failed to call cloud function:', err);
        wx.showToast({
          title: 'Failed to fetch data',
          icon: 'none'
        });
      }
    });
  },
  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.fetchuserList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.fetchuserList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})